﻿Ext.define("PSI.InPlan.SendTruckForm", {
    createUI: function (entity) {
        var me = this;
        me.entity = entity;

        var wnd = Ext.create("Ext.window.Window", {
            title: "派车",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 520,
            height: 480,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 110,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            xtype: "displayfield",
                            value: entity.planNumber,
                            fieldLabel: "计划号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.address,
                            fieldLabel: "门点"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntNumber,
                            fieldLabel: "箱号/封号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntType,
                            fieldLabel: "箱型"
                        },
                        {
                            xtype: "displayfield",
                            id: "editDeliverDT",
                            value: entity.deliverDT,
                            fieldLabel: "送货时限"
                        },
                        {
                            id: "editTruckCompany",
                            name: "truckCompany",
                            fieldLabel: "拖车公司",
                            xtype: "textfield",
                            readOnly: true
                        },
                        {
                            id: "editTruckNumber",
                            xtype: "psi_truck_trucknumberfield",
                            name: "truckNumber",
                            fieldLabel: "车号",
                            allowBlank: false,
                            blankText: "没有输入车号",
                            beforeLabelTextTpl: PSI.Const.REQUIRED
                        },
                        {
                            id: "editDriver",
                            xtype: "textfield",
                            name: "driver",
                            fieldLabel: "司机"
                        }, {
                            id: "editDriverTel",
                            xtype: "textfield",
                            name: "driverTel",
                            fieldLabel: "司机电话"
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划提箱时间",
                         //   beforeLabelTextTpl: PSI.Const.REQUIRED,
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT1Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt1Date",
                                    flex: 2
                                   // blankText: "没有输入提箱日期"
                                }, {
                                    id: "editDT1Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt1Time",

                                   // blankText: "没有输入提箱时间",
                                    flex: 2
                                }, {
                                    id: "editDT1btn",
                                    xtype: "button",
                                    flex: 1,
                                    text:'当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划出港时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT2Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt2Date",
                                    flex: 2
                                }, {
                                    id: "editDT2Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt2Time",
                                    flex: 2
                                }, {
                                    id: "editDT2btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划到卸货地时间",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT3Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt3Date",
                                    allowBlank: false,
                                    blankText: "没有输入到卸货地日期",
                                    flex: 2
                                }, {
                                    id: "editDT3Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt3Time",
                                    allowBlank: false,
                                    blankText: "没有输入到卸货地时间",
                                    flex: 2
                                }, {
                                    id: "editDT3btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划卸货开始时间",
                            layout: "hbox",
                        //    beforeLabelTextTpl: PSI.Const.REQUIRED,
                            items: [
                                {
                                    id: "editDT5Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt5Date",                                  
                                    flex: 2
                                }, {
                                    id: "editDT5Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt5Time",                         
                                    flex: 2
                                }, {
                                    id: "editDT5btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划卸货完成时间",
                            layout: "hbox",
                           // beforeLabelTextTpl: PSI.Const.REQUIRED,
                            items: [
                                {
                                    id: "editDT4Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "dt4Date",
                                    flex: 2
                                }, {
                                    id: "editDT4Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    name: "dt4Time",
                                    flex: 2
                                }, {
                                    id: "editDT4btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "textfield",
                            id: "editTripNumber",
                            name: "tripNumber",
                            fieldLabel: "趟次号",
                            allowBlank: false,
                            blankText: "没有输入趟次号",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            value: entity.planNumber
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;
        var dat = new Date();
        var cc = Ext.Date.format(dat, 'Y-m-d');
        var ss = Ext.Date.format(dat, 'H:i');
        Ext.getCmp("editDT1btn").on("click", function () {

            Ext.getCmp("editDT1Date").setValue(cc);
            Ext.getCmp("editDT1Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT2btn").on("click", function () {

            Ext.getCmp("editDT2Date").setValue(cc);
            Ext.getCmp("editDT2Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT3btn").on("click", function () {

            Ext.getCmp("editDT3Date").setValue(cc);
            Ext.getCmp("editDT3Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT4btn").on("click", function () {

            Ext.getCmp("editDT4Date").setValue(cc);
            Ext.getCmp("editDT4Time").setValue(ss);
        }, me);
        Ext.getCmp("editDT5btn").on("click", function () {

            Ext.getCmp("editDT5Date").setValue(cc);
            Ext.getCmp("editDT5Time").setValue(ss);
        }, me);
        Ext.getCmp("editTruckNumber").setLinkedEditor({
            truckCompanyEditor: Ext.getCmp("editTruckCompany"),
            driverEditor: Ext.getCmp("editDriver"),
            driverTelEditor: Ext.getCmp("editDriverTel")
        });

        Ext.getCmp("editTruckCompany").on("specialkey", function (field, e) {
            if (e.getKey() == e.BACKSPACE) {
                e.preventDefault();
            }
        }, me);

        Ext.getCmp("editTruckNumber").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Date").focus();
            }
        }, me);
        Ext.getCmp("editDT1Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Time").focus();
            }
        }, me);
        Ext.getCmp("editDT1Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Date").focus();
            }
        }, me);
        Ext.getCmp("editDT2Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Time").focus();
            }
        }, me);
        Ext.getCmp("editDT2Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Date").focus();
            }
        }, me);
        Ext.getCmp("editDT3Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Time").focus();
            }
        }, me);
        Ext.getCmp("editDT3Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT5Date").focus();
            }
        }, me);
        Ext.getCmp("editDT5Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT5Time").focus();
            }
        }, me);
        Ext.getCmp("editDT5Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Date").focus();
            }
        }, me);
        Ext.getCmp("editDT4Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Time").focus();
            }
        }, me);
    },

    show: function (parentForm, entity) {
        var me = this;
        me.entity = entity;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        Ext.getCmp("editTruckNumber").focus();

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetSendTruckInfo",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editTruckCompany").setValue(data.truckCompany);
        Ext.getCmp("editTruckNumber").setValue(data.truckNumber);
        Ext.getCmp("editDriver").setValue(data.driver);
        Ext.getCmp("editDriverTel").setValue(data.driverTel);
        if (data.tripNumber) {
            Ext.getCmp("editTripNumber").setValue(data.tripNumber);
        } else {
            Ext.getCmp("editTripNumber").setValue(this.entity.planNumber);
        }
        

        Ext.getCmp("editDT1Date").setValue(data.dt1PlanDate);
        Ext.getCmp("editDT1Time").setValue(data.dt1PlanTime);
        Ext.getCmp("editDT2Date").setValue(data.dt2PlanDate);
        Ext.getCmp("editDT2Time").setValue(data.dt2PlanTime);
        Ext.getCmp("editDT3Date").setValue(data.dt3PlanDate);
        Ext.getCmp("editDT3Time").setValue(data.dt3PlanTime);
        Ext.getCmp("editDT4Date").setValue(data.dt4PlanDate);
        Ext.getCmp("editDT4Time").setValue(data.dt4PlanTime);
        Ext.getCmp("editDT5Date").setValue(data.dt5PlanDate);
        Ext.getCmp("editDT5Time").setValue(data.dt5PlanTime);
    },

    // private
    onOK: function () {
        var me = this;

        var dt1 = me.getDT1();
        ////if (dt1 == null) {
        ////    PSI.MsgBox.showInfo("没有输入计划提箱时间");
        ////    return;
        ////}
       var dt2 = me.getDT2();
        //if (dt2) {
        //    if (dt2 < dt1) {
        //        PSI.MsgBox.showInfo("出港时间不能早于提箱时间");
        //        return;
        //    }
        //}
        var dt3 = me.getDT3();
        if (dt3==null) {
            PSI.MsgBox.showInfo("没有输入到卸货地时间");
            return;
        }
        if (dt3) {
            if (dt2 && dt1) {
                if (dt2 && dt3 < dt2) {
                    PSI.MsgBox.showInfo("到卸货地时间不能早于出港时间");
                    return;
                } else if (dt3 < dt1) {
                    PSI.MsgBox.showInfo("到卸货地时间不能早于提箱时间");
                    return;
                }
            }
        }
        var dt4 = me.getDT4();
        ////if (dt4 == null) {
        ////    PSI.MsgBox.showInfo("没有输入计划完成时间");
        ////    return;
        ////}
        //if (dt3 && dt4 < dt3) {
        //    PSI.MsgBox.showInfo("完成时间不能早于达到时间");
        //    return;
        //}
        //if (dt2 && dt4 < dt2) {
        //    PSI.MsgBox.showInfo("完成时间不能早于出港时间");
        //    return;
        //}
        //if (dt4 < dt1) {
        //    PSI.MsgBox.showInfo("完成时间不能早于提箱时间");
        //    return;
        //}
        var dt5 = me.getDT5();
        ////if (dt5 == null)
        ////{
        ////    PSI.MsgBox.showInfo("没有输入计划卸货时间");
        ////    return;
        ////}
        //if (dt5 > dt4) {
        //    PSI.MsgBox.showInfo("卸货时间不能早于完成时间");
        //    return;
        //}


        var funcSubmit = function () {
            var f = Ext.getCmp("editForm");
            var el = f.getEl() || Ext.getBody();
            el.mask("数据保存中...");
            f.submit({
                url: "/InPlan/SendTruck",
                method: "POST",
                success: function (form, action) {
                    el.unmask();
                    me.wnd.close();
                    me.parentForm.refreshInPlanGrid();
                },
                failure: function (form, action) {
                    el.unmask();
                    PSI.MsgBox.showInfo(action.result.msg, function () {
                        Ext.getCmp("editTruckNumber").focus();
                    });
                }
            });
        };


        var deliverDT = me.getDeliverTD();
        var info = "请确认是否派车?";
        if (dt4 > deliverDT) {
            info = "请确认是否派车？<br/><span style='color:red'>注意</span>: 完成时间超过了送货时限";
        }

        PSI.MsgBox.confirm(info, funcSubmit);
    },

    getDT1: function () {
        var d = Ext.getCmp("editDT1Date").getValue();
        var t = Ext.getCmp("editDT1Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT2: function () {
        var d = Ext.getCmp("editDT2Date").getValue();
        var t = Ext.getCmp("editDT2Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT3: function () {
        var d = Ext.getCmp("editDT3Date").getValue();
        var t = Ext.getCmp("editDT3Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT4: function () {
        var d = Ext.getCmp("editDT4Date").getValue();
        var t = Ext.getCmp("editDT4Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },
    getDT5: function () {
        var d = Ext.getCmp("editDT5Date").getValue();
        var t = Ext.getCmp("editDT5Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDeliverTD: function () {
        var v = Ext.getCmp("editDeliverDT").getValue();
        return Ext.Date.parse(v, "Y-m-d H:i");
    }
});